// QueryAnything: ExtendedDataGridView.cs
//
// Copyright 2007 Steve T. Pham
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
// Linking this library statically or dynamically with other modules is
// making a combined work based on this library.  Thus, the terms and
// conditions of the GNU General Public License cover the whole
// combination.
// 
// As a special exception, the copyright holders of this library give you
// permission to link this library with independent modules to produce an
// executable, regardless of the license terms of these independent
// modules, and to copy and distribute the resulting executable under
// terms of your choice, provided that you also meet, for each linked
// independent module, the terms and conditions of the license of that
// module.  An independent module is a module which is not derived from
// or based on this library.  If you modify this library, you may extend
// this exception to your version of the library, but you are not
// obligated to do so.  If you do not wish to do so, delete this
// exception statement from your version.using System;

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Data;

namespace SteveTPham.QueryAnything.Controls
{
	class ExtendedDataGridView : DataGridView
	{
		private int offset1 = 15;
		private int offset2 = 20;
		private ContextMenuStrip contextMenuStrip1;
		private System.ComponentModel.IContainer components;
		private ToolStripMenuItem mnuAnalyze;
		private ToolStripMenuItem mnuResize;
		private ToolStripMenuItem mnuOrderBy;

		private object dataSource = null;

		public ExtendedDataGridView()
			: base()
		{
			InitializeComponent();
			base.ContextMenuStrip = contextMenuStrip1;
			base.EditMode = DataGridViewEditMode.EditOnEnter;
		}

		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
			this.mnuAnalyze = new System.Windows.Forms.ToolStripMenuItem();
			this.mnuResize = new System.Windows.Forms.ToolStripMenuItem();
			this.mnuOrderBy = new System.Windows.Forms.ToolStripMenuItem();
			this.contextMenuStrip1.SuspendLayout();
			((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
			this.SuspendLayout();
			// 
			// contextMenuStrip1
			// 
			this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.mnuAnalyze,
            this.mnuResize,
            this.mnuOrderBy});
			this.contextMenuStrip1.Name = "contextMenuStrip1";
			this.contextMenuStrip1.Size = new System.Drawing.Size(160, 70);
			this.contextMenuStrip1.Click += new System.EventHandler(this.contextMenuStrip1_Click);
			// 
			// mnuAnalyze
			// 
			this.mnuAnalyze.Name = "mnuAnalyze";
			this.mnuAnalyze.Size = new System.Drawing.Size(159, 22);
			this.mnuAnalyze.Text = "Analyze";
			this.mnuAnalyze.Click += new System.EventHandler(this.mnuAnalyze_Click);
			// 
			// mnuResize
			// 
			this.mnuResize.Name = "mnuResize";
			this.mnuResize.Size = new System.Drawing.Size(159, 22);
			this.mnuResize.Text = "Resize Columns";
			this.mnuResize.Click += new System.EventHandler(this.mnuResize_Click);
			// 
			// mnuOrderBy
			// 
			this.mnuOrderBy.Name = "mnuOrderBy";
			this.mnuOrderBy.Size = new System.Drawing.Size(159, 22);
			this.mnuOrderBy.Text = "Order By...";
			this.mnuOrderBy.Click += new System.EventHandler(this.mnuOrderBy_Click);
			this.contextMenuStrip1.ResumeLayout(false);
			((System.ComponentModel.ISupportInitialize)(this)).EndInit();
			this.ResumeLayout(false);

		}

		private int LengthOf(object value)
		{
			return value == null ? 0 : value.ToString().Length;
		}

		protected override void OnRowPostPaint(DataGridViewRowPostPaintEventArgs e)
		{
			string format = "{0:D" + LengthOf(base.RowCount) + "}";
			string value = string.Format(format, e.RowIndex + 1);

			// adjust the width to fit the row number
			SizeF size = e.Graphics.MeasureString(value, base.Font);
			base.RowHeadersWidth = Math.Max(base.RowHeadersWidth, (int)size.Width + offset2);

			// draw the row number
			Rectangle r = e.RowBounds;
			PointF p = new PointF(r.Location.X + offset1, r.Top + ((r.Height - size.Height) / 2));
			e.Graphics.DrawString(value, base.Font, SystemBrushes.ControlText, p);

			base.OnRowPostPaint(e);
		}

		protected override void OnDataSourceChanged(EventArgs e)
		{
			if (base.DataSource is DataSet)
				this.dataSource = base.DataSource;

			base.OnDataSourceChanged(e);
		}

		private void contextMenuStrip1_Click(object sender, EventArgs e)
		{
		}

		private void mnuResize_Click(object sender, EventArgs e)
		{
			base.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
		}

		private void mnuAnalyze_Click(object sender, EventArgs e)
		{
			MessageBox.Show("Analyze Data");
		}

		private void mnuOrderBy_Click(object sender, EventArgs e)
		{
			MessageBox.Show("Order By...");
			return;

			try
			{
				DataTable dt = null;
				if (this.dataSource is DataSet)
				{
					DataSet ds = this.dataSource as DataSet;
					if (ds == null)
						return;
					dt = ds.Tables[base.DataMember];
				}


				DataView dv = dt.DefaultView;
				dv.Sort = "";

				base.DataSource = dv;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
	}
}